package com.sense.kukoo.biz_me.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.adapter.FollowingsListAdapter
import com.sense.kukoo.biz_me.databinding.BizMeActivityFollowingsListBinding
import com.sense.kukoo.biz_me.vm.FollowingsListViewModel
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.http.bean.UserSearchBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.sense.kukoo.lib_logger.LoggerManager
import com.zy.multistatepage.state.SuccessState

@Route(path = ARouterUrl.ROUTE_ME_MODULE_FOLLOWINGS_LIST_ACTIVITY)
class FollowingsListActivity: BaseMvvmActivity<FollowingsListViewModel, BizMeActivityFollowingsListBinding>() {

    private val TAG: String = javaClass.simpleName
    private val mAnimHelper = BottomMotionAnimHelper()
    private var type_isFans: Int = -1

    private lateinit var mAdapter: FollowingsListAdapter

    @JvmField
    @Autowired(name = ARouterUrl.KEY_FROM)
    var from: String? = null //类型: 1/查询粉丝数据 0/查询关注数据

    override fun responseVM(action: Int) {}

    override fun getLayoutId(): Int = R.layout.biz_me_activity_followings_list

    override fun getViewModelClass(): Class<FollowingsListViewModel> = FollowingsListViewModel::class.java

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .navigationBarColor(com.sense.kukoo.common_design.R.color.bg_background)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .fitsSystemWindows(false)
            .init()
    }

    override fun initData() {
        ARouter.getInstance().inject(this)
        type_isFans = from!!.toInt()
        mViewModel.setInitData(type_isFans)
        //
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_back){
            mAnimHelper.animateToFinish()
        }
        bindAction(mViewModel.action_toChannelPage){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_CHANNEL_ACTIVITY)
                .withString(ARouterUrl.KEY_FROM, "${it.id}")
                .navigation()
        }
        bindAction(mViewModel.action_network_error){
            mBinding.multiStateContainer.show<NetworkErrorState> {
                it.onRetry { mViewModel.onErrorRetry() }
            }
        }
        bindAction(mViewModel.action_finishRefres){
            mBinding.refreshLayout.finishRefresh()
        }
        bindAction(mViewModel.action_finishLoadMore){
            mBinding.refreshLayout.finishLoadMore()
        }
        bindData(mViewModel.data_enableLoadMore){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
        bindData(mViewModel.data_followingsContent){
            mBinding.multiStateContainer.show<SuccessState>()
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        //
        when (type_isFans) {
            0 -> {
                mBinding.tvTitle.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.me_followings_list_title)
            }
            1 -> {
                mBinding.tvTitle.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.me_followers_list_title)
            }
        }
        //
        mAdapter = FollowingsListAdapter(mutableListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter
        //
        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.refreshList(false)
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.loadMoreList()
        }
        //弹出动画
        val first = ApplicationContext.dp2px(800F).toFloat()
        mAnimHelper.init(mBinding.flFollowingsListRoot, mBinding.clFollowingsListContentRoot, first){ finish() }
        mAnimHelper.animateToStart()
        //
        mViewModel.refreshList(true)
    }

    override fun onBackPressed() {
        mAnimHelper.animateToFinish()
    }

    private val mListener = object : FollowingsListAdapter.FollowingsEventListener {
        override fun onBtnFollowClick(data: UserSearchBean) {
            mViewModel.onBtnFollowClick(data)
        }

        override fun onBtnChannelClick(data: UserSearchBean) {
            mViewModel.onBtnChannelClick(data)
        }
    }
}